package com.zlx.controller;

import com.zlx.entities.Student;
import com.zlx.service.StudentServcue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author 周罗校
 * @Description
 * @ClassName StudentController
 * @date 2020.10.15 18:20:53
 * 该版权归周罗校所有，最终解释权以官方为准
 */

@Controller
@RequestMapping("/student/")
public class StudentController {
    @Autowired
    private StudentServcue studentServcue;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(Student student, Model model){

        Student result = null;
        if (student==null){
            model.addAttribute("error","用户或密码不能为空");
            return "forward:/index.jsp";
        }else {
            result=studentServcue.findStudentByNameAndPs(student);
            if (result==null){
                model.addAttribute("error","用户名或密码错误");
                return "forward:/index.jsp";
            }else {
                return "redirect:/student/list";
            }
        }
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(Model model){
        model.addAttribute("list",studentServcue.listStudent());
        return "list";
    }

    @RequestMapping(value = "/toAdd",method = RequestMethod.GET)
    public String toAdd(){
        return "add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(Student student){
        studentServcue.addStudent(student);
        return "redirect:/student/list";
    }

    @RequestMapping(value = "/jsonAdd",method = RequestMethod.GET)
    @ResponseBody
    public Student jsonAdd(Student student){
        studentServcue.addStudent(student);
        return student;
    }
}
